package com.example.javamaday.leetcode;

import com.example.javamaday.leetcode.base.TreeNode;

public class LeetCodeTest0129 {

    public static void main(String[] args) {

    }

    private int result = 0;

    public int sumNumbers(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return root.val;
        }
        scanTree(root, root.val);

        return result;
    }

    private void scanTree(TreeNode treeNode, int val) {
        // 为叶子节点
        if (treeNode.left == null && treeNode.right == null) {
            System.out.println(val);
            result+= val;
            return;
        }
        if (treeNode.left != null) {
            scanTree(treeNode.left, val * 10 + treeNode.left.val);
        }
        if (treeNode.right != null) {
            scanTree(treeNode.right, val * 10 + treeNode.right.val);
        }
    }

}
